package com.wise.service.cms.content.applicationarticle;

import com.wise.entities.content.ApplicationArticle;
import com.wise.framework.basic.DateTimeUtil;
import com.wise.framework.daomodel.DaoOne;
import com.wise.framework.daomodel.DaoSave;
import com.wise.framework.exception.LogicException;
import com.wise.framework.session.CurrentUserId;
import com.wise.service.cms.content.article.ArticleTools;
import com.wise.summer.v1.cms.content.applicationarticle.CopyApplicationArticle;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import static com.wise.model.content.ContentErrMsgs.Err_001600250007;
import static com.wise.model.content.ContentErrMsgs.Err_001600250019;

/**
 * @author hejiawei
 * @date 2020/10/27 12:33
 */
@Service
@SummerService
public class CopyApplicationArticleService implements SummerServiceBean<CopyApplicationArticle> {
    @Override
    public void sum(CopyApplicationArticle summer) {
        ApplicationArticle article = DaoOne.s(ApplicationArticle.class, summer.getId(), Err_001600250007);

        // 别名不可重复
        summer.setAlias(ArticleTools.toNullAlias(summer.getAlias()));
        if (ArticleTools.checkApplicationAlias(summer.getAlias())) {
            throw new LogicException(this.getClass(), Err_001600250019);
        }

        ApplicationArticle ar = new ApplicationArticle();
        BeanUtils.copyProperties(article, ar, ApplicationArticle._id);
        ar.setTitle(summer.getTitle());
        ar.setAlias(summer.getAlias());
        ar.setCreationDate(DateTimeUtil.timestampNow());
        ar.setCreatedBy(CurrentUserId.s(true));
        ar.setLastUpdateBy(null);
        ar.setLastUpdateDate(null);

        DaoSave.s(ar);

        summer.setSummerResult(true);
    }
}
